import { routes, superRoutes } from './routes';
import { useStore } from '@/store/useStore';
import router from './index';

export default () => {
  const store = useStore();
  let type = store.type;
  if (type == 1) {
    superRoutes.forEach(route => {
      router.addRoute(route);
    })
    // 把路由表存入pinia
    store.$patch(state => {
      state.routes = superRoutes;
    });

  } else {
    routes.forEach(route => {
      router.addRoute(route);
    })
    store.$patch(state => {
      state.routes = routes;
    });
  }

  // 路由守卫(路由拦截)
  // 白名单: 无需登录就可以访问的页面
  let whiteList = ['/login']
  router.beforeEach((to, from, next) => {
    const store = useStore();
    // 若已登录
    if (store.isLogin) {
      if (to.path === '/login') {
        next('/home/index');
      } else {
        next();
      }
    } else {
      // 没登录,查看是否在白名单内, 是=>放行, 不是跳转到登录页面
      if (whiteList.includes(to.path)) {
        next();
      } else {
        next('/login');
      }
    }
  })
}